package com.xusheng.msmservice.service.impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.xusheng.msmservice.service.MsmService;
import com.xusheng.msmservice.utils.RandomUtils;
import com.xusheng.msmservice.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

//@Service
/*public class MsmServiceImpl implements MsmService {

    //发送短信的方法
    @Override
    public boolean send(Map<String, Object> param, String phone) {
        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI4FvvVEWiTJ3GNJJqJnk7", "9st82dv7EvFk9mTjYO1XXbM632fRbG");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定的参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //设置发送相关的参数
        request.putQueryParameter("PhoneNumbers",phone); //手机号
        request.putQueryParameter("SignName","我的谷粒在线教育网站"); //申请阿里云 签名名称
        request.putQueryParameter("TemplateCode","SMS_180051135"); //申请阿里云 模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递

        try {
            //最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}*/
   /* public  class MsmServiceImpl implements MsmService {



    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String send(String phoneNum) {

      *//*  //判断redis中是否有验证码或有效期未过
        String code = redisTemplate.opsForValue().get(phoneNum);
        if (!StringUtils.isEmpty(code)) {
            return code;
        }*//*

        // 如果没有则进行腾讯云短信发送
        //生成6位随机验证码(验证码参数需要数组对象) 可以随意给出6为数字 我这里使用随机数
        String[] params = {RandomUtils.getRandom()};

        try {
            //创建短信发送对象
            SmsSingleSender ssender = new SmsSingleSender(TencentUtils.APPID, TencentUtils.APPKEY);

            SmsSingleSenderResult result = ssender.sendWithParam(TencentUtils.NATIONCODE, phoneNum,
                    TencentUtils.TEMPLATECODE, params, TencentUtils.SIGN, "", "");

            //验证是否发送成功，发送成功将验证码返回 等待用户输入从而进行数据验证
            //{"result":0,"errmsg":"OK","ext":"","sid":"2433:22806866016300548263905129","fee":1,"isocode":"CN"}
            if (result.result == 0) {
                //发送成功 把验证码存入redis 并设置有效期5分钟
                redisTemplate.opsForValue().set(phoneNum, params[0], 5, TimeUnit.MINUTES);
                return params[0];
            }

        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}*/
@Service
public class MsmServiceImpl implements MsmService {

    //发送短信的方法
    @Override
    public boolean send(Map<String, Object> param, String phone) {
        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                //DefaultProfile.getProfile("default", "LTAI4Fy76hybzhPUECfCt19E", "jJZ2Nk9JXt8YH94UG3ZIApy7ltRuXe");

        DefaultProfile.getProfile("default", "LTAI5tNm8X4UBpz3m9eGiamK", "dXc2VUvuQTxkXLOLlYejiIGs9YtqGh");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定的参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //设置发送相关的参数
        request.putQueryParameter("PhoneNumbers",phone); //手机号
        request.putQueryParameter("SignName","我的谷粒在线教育网站"); //申请阿里云 签名名称
        request.putQueryParameter("TemplateCode","SMS_154950909"); //申请阿里云 模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递

        try {
            //最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}

